package com.eascs.web.o2o.mq;

import java.io.IOException;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.notify.constant.ModelResult;
import com.eascs.web.o2o.mq.service.ProducerService;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

/**
 * 发送者
 * 
 * @author user
 *
 * @param <T>
 */
@Service
public class LoanProducer extends EndPoint implements ProducerService {
	private static final Logger logger = LoggerFactory.getLogger(LoanProducer.class);

	/*
	 * 生产消息
	 * param：（Message）transType消息名称，body消息内容  (传入的实体类)
	 */
	@Override
	public ModelResult<Boolean> sendMessage(Message message) {
		ModelResult<Boolean> model= new ModelResult<Boolean>();
		try {
			Channel channel = buildChannel();
			if (channel == null) {
				logger.info("loan producer start failed.");
				model.withModel(false);
				model.withError("loan producer start failed", "loan producer start failed");
				return model; 
			}
			logger.info("loan producer start success.");
			String sendMessage = JSON.toJSONString(message);
			channel.basicPublish("", INTERFACE_TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, sendMessage.getBytes());
			model.withModel(true);
		} catch (IOException e) {
			model.withModel(false);
			model.withError("LoanProducer,err", "LoanProducer,err");
			logger.error("LoanProducer,err", e);
		}
		return model;
	}
}
